//
//  SetUp.m
//  HYHOME
//
//  Created by Mac on 16/1/29.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "SetUp.h"
#import "Service.h"
#import "DKNightVersion.h"
#import "Like.h"
#import "UIImageView+WebCache.h"
#import "ME.h"

@interface SetUp ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)UIImageView *backgroundview;
@property (nonatomic,retain)NSMutableArray *array;

@property (nonatomic, retain)NSMutableArray *arrfile;
@property (nonatomic, assign) int count;
@property (nonatomic, retain)NSString *str;
@property (nonatomic,retain)UISwitch *sw;
@property (nonatomic,retain)NSMutableArray *arrI;
@end

@implementation SetUp

- (void)viewDidLoad {
    [super viewDidLoad];
    self.count = 0;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    
    self.view.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    
    self.title = @"设置";
    

    [self createTableView];
    [self handleData];

    
    
   
    //设置标题属性
    NSDictionary *attribute = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]};
    //设置属性
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:attribute forState:UIControlStateNormal];
    //设置登录状态
    [self viewWillAppear:YES];
    
}
- (void)handleData{
    
    NSLog(@"%lu", (unsigned long)[[SDImageCache sharedImageCache] getSize]);


    NSLog(@"%@", NSHomeDirectory());
    self.str = [NSString stringWithFormat:@"%0.2fM", ([[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0))];
    self.array = @[
                   @[@{@"name":@"建议与反馈",@"image":@"iconfont-jianyi"},
                     @{@"name":@"当前版本",@"image":@"banbenhao"}],
                   @[@{@"name":@"服务条款",@"image":@"terms"},
                     @{@"name":@"清空缓存",@"image":@"appxiugaiicon29",@"number": self.str},
                     @{@"name":@"夜间",@"image":@"night"}]].mutableCopy;
    
    
    self.arrI = @[@"banbenhao",@"terms",@"appxiugaiicon29",@"banbenhao",@"jianyi"].mutableCopy;


}
- (void)createTableView{

   self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];

    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.dk_separatorColorPicker = DKColorWithRGB(0xaaaaaa, 0x313131);
    [self.view addSubview:self.tableView];
    
  
    
    // 分隔线颜色
    self.tableView.separatorColor = [UIColor lightGrayColor];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self.tableView release];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    
    NSArray *arr = self.array[section];
    return arr.count;
    


}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 50;
    } else{
    
        return 50;
    
    }
 
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    static NSString *pool1 = @"resuse";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool1] autorelease];

    }
    
   
    
    NSArray *arr = [self.array objectAtIndex:indexPath.section];
    NSDictionary *dic = arr[indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"name"];
    cell.detailTextLabel.text = [dic objectForKey:@"number"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:[dic objectForKey:@"image"]];
    //cell.imageView.image = [dic objectForKey:@"image"];
    cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor lightGrayColor]);
    
    cell.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
   
    if (indexPath.section ==1 && indexPath.row ==2 ) {
        self.sw = [[UISwitch alloc] initWithFrame:CGRectZero];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {
            self.sw.on = YES;
        }else {
            self.sw.on = NO;
            
        }
        [self.sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = self.sw;
      
        [self.view addSubview:self.sw];
        

        self.view.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
     
        
    }

    
    return cell;
    
    

}
- (void)switchAction:(UISwitch *)sw{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    if (self.sw.on && [DKNightVersionManager currentThemeVersion] == DKThemeVersionNormal) {
        
        
        [DKNightVersionManager nightFalling];
    } else {
        [DKNightVersionManager dawnComing];
    }
    
    [user setBool:sw.on forKey:@"ISNIGHT"];
    [user synchronize];
    
    
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    
    
    /**让 TableView 进入/退出 编辑状态*/
    
    [self.tableView setEditing:editing animated:animated];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   if (indexPath.section == 0 && indexPath.row ==0){
    
       
       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该功能暂未开放" preferredStyle:UIAlertControllerStyleAlert];
       
       
       UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style: 0 handler:^(UIAlertAction * _Nonnull action) {
           
       }];
       
       [alert addAction:action];
       
       [self presentViewController:alert animated:YES completion:^{
           
       }];
       
    }else if (indexPath.section == 0 && indexPath.row == 1){
      
        
        NSLog(@"asd");
        
        
    }else if (indexPath.section == 1 && indexPath.row == 0){
        
        
        Service *service = [[Service alloc]init];
        
        [self.navigationController pushViewController:service animated:YES];
        
        
    }else if (indexPath.section == 1 && indexPath.row == 1) {
        
      
        [self performSelectorOnMainThread:@selector(clear) withObject:nil waitUntilDone:YES];
        NSLog(@"444");
        
        
    }else {
    
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    
    
}

- (void)clear{
   NSLog(@"清理成功");
    
    [[SDImageCache sharedImageCache] clearDisk];
    
    
    self.str = [NSString stringWithFormat:@"%0.2fM", ([[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0))];
    self.array = @[
                   @[@{@"name":@"建议与反馈",@"image":@"iconfont-jianyi"},
                     @{@"name":@"当前版本",@"image":@"banbenhao"}],
                   @[@{@"name":@"服务条款",@"image":@"terms"},
                     @{@"name":@"清空缓存",@"image":@"appxiugaiicon29",@"number": self.str},
                     @{@"name":@"夜间",@"image":@"night"}]].mutableCopy;
    
    [self.tableView reloadData];

}



//
//- (void)like{
//    if (_loginState) {
//        Like *activity = [[Like alloc] init];
//        //设置push隐藏tabbar
//        activity.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:activity animated:YES];
//    }else{
//        //登录
//        ME *login = [[ME alloc] init];
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
//        //模态显示
//        [self.navigationController presentViewController:nav animated:YES completion:nil];
//    }
//}

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//    //确认注销
//    if (alertView.tag == 101 && buttonIndex == 1) {
//        //注销
//        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//        [user setBool:NO forKey:@"loginState"];
//        //设置登录状态
//        [self viewWillAppear:YES];
//    }
//}
//
//

//- (void)loginOrExit:(UIBarButtonItem *)sender{
//    if (_loginState) {
//        //注销
//        //初始化弹窗
//        
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确认注销" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//        alertView.tag = 101;
////        //显示弹框
//        [alertView show];
//                
//    }else{
//        //登录
//         ME *login = [[ME alloc] init];
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
//        //模态显示
//        [self.navigationController presentViewController:nav animated:YES completion:nil];
//    }
//}
//- (void)viewWillAppear:(BOOL)animated{
//    //获取登录状态
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSString *str = nil;
//    self.loginState = [user boolForKey:@"loginState"];
//    if (_loginState == YES) {
//        str = @"注销";
//    }else{
//        str = @"登录";
//    }
//    self.navigationItem.rightBarButtonItem.title = str;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
